<?php
/**
 * Created by PhpStorm.
 * User: User
 * Date: 2017/11/27
 * Time: 11:00
 */

namespace Kinfy;

class Config
{
    protected static $conf = [];
    protected static $base_dir = '';

    public static function init($dir)
    {
        self::$base_dir = $dir;
    }

    public static function get($key)
    {

        $k = explode('.', $key, 3);

        if (!isset(self::$conf[$k[0]])) {
            self::$conf[$k[0]] = include self::$base_dir . $k[0] . '.php';
        }

        switch (count($k)) {
            case 1:
                return self::$conf[$k[0]];
            case 2:
                return isset(self::$conf[$k[0]][$k[1]]) ?
                    self::$conf[$k[0]][$k[1]] : null;
            case 3:
                return isset(self::$conf[$k[0]][$k[1]][$k[2]]) ?
                    self::$conf[$k[0]][$k[1]][$k[2]] : null;
        }

    }

    public static function set($key, $value)
    {
        $k = explode('.', $key, 3);
        switch (count($k)) {
            case 1:
                self::$conf[$k[0]] = $value;
                break;
            case 2:
                self::$conf[$k[0]][$k[1]] = $value;
                break;
            case 3:
                self::$conf[$k[0]][$k[1]][$k[2]] = $value;
                break;
        }
    }
}
